// sub/user/delivery-order/delivery-order.js
import {initLoadMoreMixin} from '../../../utils/list-load-more'

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    itemList: [],
  },

  tabChange(event){
    this.setData({ tabActive: event.detail.index })
    this.resetInit();
  },

  initData() {
    let uid = app.getUser().id;
    return app.requestUrl({
      url: '/wxapi/deliveryorder/list',
      data:{
        limit: this.data.pages.perPage,
        offset: (this.data.pages.currPage - 1) * this.data.pages.perPage,
        dmid: uid,
        status: this.data.tabActive==0 ? 2 : 3,//2配送中，3已送达
        dispatchtype: 2
      },
      load: 1
    }).then(res => {
      const {
        rows,
        total
      } = res.data.data;
      this.setData({
        itemList: this.data.items.concat(rows)
      })
      return {
        pageCount: rows.length,
        totalCount: total
      };
    })
  },

  //确认送达
  confirmService(event){
    let index = event.currentTarget.dataset.index;
    let id = event.currentTarget.dataset.id;
    let uid = event.currentTarget.dataset.uid;
    app.requestUrl({
      url: '/wxapi/deliveryman/confirmReceived',
      data:{
        orderId: id,
        userId: uid
      }
    }).then(res => {
      this.setData({ ['itemList[' + index + '].status']: 3 })
    })
  },

  //查看详情
  toDeliveryInfo(event){
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/sub/user/delivery-info/delivery-info?id=' + id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    initLoadMoreMixin.call(this)
    this.resetInit();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})